/*
 * AnimalUnit.hpp
 *
 *  Created on: 2013-1-17
 *      Author: heqiu
 */

#ifndef ANIMALUNIT_HPP_
#define ANIMALUNIT_HPP_
#include "mycomm.h"
class AnimalUnit {
public:
	AnimalUnit() {
		m_iId = 0;
		m_iType = 0;
		m_pSDL_Surface = NULL;
		m_uiBeginMSec = 0;
		m_uiSpeed = 1;
	}
	bool loadImage(string & sName) {
		SDL_Surface* loadedImage = NULL;
		SDL_Surface* optimizedImage = NULL;
		loadedImage = IMG_Load(sName.c_str());
		if (loadedImage != NULL) {
			optimizedImage = SDL_DisplayFormat(loadedImage);
			SDL_FreeSurface(loadedImage);
			if (optimizedImage != NULL) {
				SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY,
						SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
			}
		}
		m_pSDL_Surface = optimizedImage;
		return false;
	}
	void moving(uint32_t uiMSec, int32_t & iX, int32_t & iY) {
		if (m_uiBeginMSec == 0) {
			m_uiBeginMSec = uiMSec;
			iX = m_pPos.m_x;
			iY = m_pPos.m_y;
			m_fXshift = 0;
			m_fYshift = 0;
			return;
		}
		uint32_t uiNum = (uiMSec - m_uiBeginMSec) / m_uiSpeed;
		if (m_vctPath.size() > 0) {
			uint32_t uiPos = uiNum % m_vctPath.size();
			m_pPos.m_x = m_vctPath.at(uiPos).first;
			m_pPos.m_y = m_vctPath.at(uiPos).second;
			uint32_t uiShift = (uiNum + 1) % m_vctPath.size();
			float fShift = (uiMSec - m_uiBeginMSec) % m_uiSpeed * 1.0
					/ m_uiSpeed;
			if (m_vctPath.at(uiShift).first < m_pPos.m_x) {
				m_fXshift = -fShift;
			} else if (m_vctPath.at(uiShift).first > m_pPos.m_x) {
				m_fXshift = fShift;
			} else {
				m_fXshift = 0;
			}
			if (m_vctPath.at(uiShift).second < m_pPos.m_y) {
				m_fYshift = -fShift;
			} else if (m_vctPath.at(uiShift).second > m_pPos.m_y) {
				m_fYshift = fShift;
			} else {
				m_fYshift = 0;
			}
		}
		iX = m_pPos.m_x;
		iY = m_pPos.m_y;
		//printf("uiNum=%d size=%d x=%d y=%d\n", uiNum, m_vctPath.size(),m_pPos.m_x, m_pPos.m_y);
	}

	MyPoint m_pPos;
	uint32_t m_iId;	//建筑标号
	uint32_t m_iType;	//建筑类别
	SDL_Surface* m_pSDL_Surface;
	uint32_t m_uiBeginMSec;	//开始移动时刻点
	uint32_t m_uiSpeed;	//几毫秒移动一个坐标
	float m_fXshift;
	float m_fYshift;
	vector<pair<uint32_t, uint32_t> > m_vctPath;
};

#endif /* ANIMALUNIT_HPP_ */
